package com.demo.demos.demo1.service;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.demos.demo1.mapper.UserMapper;
import com.demo.demos.demo1.model.po.User;
import com.demo.demos.demo1.model.query.UserPQuery;
import com.demo.result.PageDTO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zken
 * @since 2024/11/27 16:01:02
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    /**
     * 基础分页请求--管理员
     */
    @Override
    public PageDTO<User> pageQuery(UserPQuery query) {
        // 1.添加排序规则
        // 使用内置的函数转换并且 设置规则-这里为了方便扩展和使用先进行创建
        List<OrderItem> orderItems = query.toMpOrderItems();

        // 获取分页对象和排序情况
        Page<User> page = query.toMpPageDefaultSortByIdAsc(orderItems.toArray(new OrderItem[0]));
    /*
    查询
    */
        Page<User> p = baseMapper.selectPageVO(page, query);

        // 获取数据信息
        return PageDTO.of(p, User.class);
    }


    @Override
    public User saveUser(User user) {
        user.setId(null);
        baseMapper.insert(user);
        return user;
    }

    @Override
    public User updateUserById(User user) {
        baseMapper.updateById(user);
        return user;
    }


}
